import axios, {AxiosError, AxiosRequestConfig, AxiosResponse} from 'axios';
import {ElNotification} from "element-plus";

const request = axios.create({
    baseURL: 'http://localhost:18000',
    timeout: 5000
})

request.interceptors.request.use((config:AxiosRequestConfig) => {
    const token: String ='JoneUser'
    return {
        ...config,
        headers:{
            'Authori-zation': token
        }
    }
})

request.interceptors.response.use((response: AxiosResponse)=>{
    const { data, config } = response;
    if (data.code && data.code !== '200') {
        ElNotification({
            title: '温馨提示',
            message: data.message,
            type: 'info'
        }); // 此处也可以使用组件提示报错信息
        return Promise.reject(data)
    }
    return data;
},(error:AxiosError)=>{
    const {message}=error;
    debugger;
    ElNotification({
        title: '请求失败',
        message: message,
        type: 'error',
    });
})

export default request;
